<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class BaseDB extends DB
{
    /**
     * 基本DB方式操作数据库
     */

    protected $tAdmin = 'admin'; //管理员
    protected $tFront = 'front'; //前台菜单
    protected $tLang = 'lang_view'; //前台页面语言
    protected $tImgs = 'imgs'; //图片集合
    protected $tVideo = 'video'; //大会视频
    protected $tVideoTmp = 'video_tmp'; //视频临时表
    protected $tClient = 'client_tongji'; //客户端统计
    protected $tMeet = 'meet'; //大会
    protected $tFocus = 'meet_focus'; //大会聚焦
    protected $tSpeaker = 'meet_speaker'; //大会演讲嘉宾
    protected $tFile = 'meet_file'; //资料
    protected $tLearn = 'meet_learn'; //学术委员会

    public function __construct()
    {
    }

    /**
     * JSON转化为数组
     */
    public function jsonToArr($str) {
        if (is_string($str)) {
            $str = json_decode($str);
        }
        $arr = array();
        foreach($str as $k=>$v) {
            if(is_object($v) || is_array($v)) {
                $arr[$k] = $this->jsonToArr($v);
            } else {
                $arr[$k] = $v;
            }
        }
        return $arr;
    }

    /**
     * 生成随机字符串，默认数字字母混合
     * $numeric==0纯数字
     */
    public function getRandStr($length=6,$numeric=1)
    {
        if(!$numeric) {
            //纯数字
            $str = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            //数字、字母组合
            $str = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $str .= $chars[mt_rand(0, $max)];
            }
        }
        return $str;
    }
}